package ch.zbw.pra.sysventory.domain.dbhandling;

import java.util.Iterator;
import java.util.List;

import ch.zbw.pra.sysventory.domain.dbconnector.IDBConnector;
import ch.zbw.pra.sysventory.domain.model.Nic;
import ch.zbw.pra.sysventory.domain.model.Scanversion;

public class SearchInScanversionsFromDB {

	private static final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(SearchInScanversionsFromDB.class);

	private List<Scanversion> scanversions;
	private List<Nic> nics;
	private IDBConnector dbconnector;
	private GetAllScanversionsFromDB getAllScanversionsFromDB;

	public SearchInScanversionsFromDB(IDBConnector dbconnector) {
		this.dbconnector = dbconnector;
	}

	public List<Scanversion> searchForScanversionsInDB(String computerSearchText,
			String ipSearchText, String computerTypSearchText, String processorSearchText,
			String osSearchText) {
		this.getAllScanversionsFromDB = new GetAllScanversionsFromDB(this.dbconnector);
		this.scanversions = this.getAllScanversionsFromDB.getAllScanversions();
		this.scanversions = searchComputerName(computerSearchText, this.scanversions);
		this.scanversions = searchIP(ipSearchText, this.scanversions);
		this.scanversions = searchComputerTyp(computerTypSearchText, this.scanversions);
		this.scanversions = searchProcessor(processorSearchText, this.scanversions);
		this.scanversions = searchOSName(osSearchText, this.scanversions);
		log.debug("Searched for: " + computerSearchText + ", " + ipSearchText + ", "
				+ computerTypSearchText + ", " + processorSearchText + ", " + osSearchText);
		log.debug("Found " + this.scanversions.size() + " Scanversions");
		return this.scanversions;
	}

	private List<Scanversion> searchOSName(String osSearchText, List<Scanversion> sortedScanversions) {
		for (Iterator<Scanversion> i = sortedScanversions.iterator(); i.hasNext();) {
			Scanversion scanversion = i.next();
			boolean remove = true;
			if (scanversion.getComputer().getOs().getNameOperatingSystem().toLowerCase()
					.contains(osSearchText.toLowerCase())) {
				remove = false;
			}
			if (remove) {
				i.remove();
			}
		}
		return sortedScanversions;
	}

	private List<Scanversion> searchProcessor(String processorSearchText,
			List<Scanversion> sortedScanversions) {
		for (Iterator<Scanversion> i = sortedScanversions.iterator(); i.hasNext();) {
			Scanversion scanversion = i.next();
			boolean remove = true;
			if (scanversion.getComputer().getProcessor().getNameProcessor().toLowerCase()
					.contains(processorSearchText.toLowerCase())) {
				remove = false;
			}
			if (remove) {
				i.remove();
			}
		}
		return sortedScanversions;
	}

	private List<Scanversion> searchComputerTyp(String computerTypSearchText,
			List<Scanversion> sortedScanversions) {
		for (Iterator<Scanversion> i = sortedScanversions.iterator(); i.hasNext();) {
			Scanversion scanversion = i.next();
			boolean remove = true;
			if (scanversion.getComputer().getTypeComputer().toLowerCase()
					.contains(computerTypSearchText.toLowerCase())) {
				remove = false;
			}
			if (remove) {
				i.remove();
			}
		}
		return sortedScanversions;
	}

	private List<Scanversion> searchIP(String ipSearchText, List<Scanversion> sortedScanversions) {
		for (Iterator<Scanversion> i = sortedScanversions.iterator(); i.hasNext();) {
			Scanversion scanversion = i.next();
			boolean remove = true;
			this.nics = scanversion.getComputer().getNics();
			for (Nic nic : this.nics) {
				if (nic.getIpv4Nic().toLowerCase().contains(ipSearchText.toLowerCase())) {
					remove = false;
				}
			}
			if (remove) {
				i.remove();
			}
		}
		return sortedScanversions;
	}

	private List<Scanversion> searchComputerName(String computerSearchText,
			List<Scanversion> sortedScanversions) {
		for (Iterator<Scanversion> i = sortedScanversions.iterator(); i.hasNext();) {
			Scanversion scanversion = i.next();
			boolean remove = true;
			if (scanversion.getComputer().getHostNameComputer().toLowerCase()
					.contains(computerSearchText.toLowerCase())) {
				remove = false;
			}
			if (remove) {
				i.remove();
			}
		}
		return sortedScanversions;
	}

}
